package formin.powerbar.core;
import formin.powerbar.data.*;
public class PowerBarCore {
	
	static private PowerBarCore core;
	private PowerBarTimeService timeService;
	private PowerBarGUI gui;
	private PowerBarFileIO fileIO;
	private PowerBarWidgetInterface widgetInterface;
	
	private PlugCollection plugs;
	private SettingsData settingsData;
	
	//core constuctor
	public PowerBarCore(){
		
		//create the fileIO handler and read in the settings file
		fileIO = new PowerBarFileIO();
		settingsData = fileIO.readSettings();
		
		//create other services
		timeService = new PowerBarTimeService();
		widgetInterface = new PowerBarWidgetInterface();
		
		//create gui
		gui = new PowerBarGUI(this, settingsData);
		
		//read in plug data from data file
		plugs = fileIO.readPlugData(settingsData.getNumbPlugs());
		
		//update gui from the plug data model
		for(int i = 0; i < settingsData.getNumbPlugs(); i++){
			gui.createPlugGui(plugs.getPlug(i));
			gui.setOnOffStringFor(i, plugs.getPlug(i).getPlugString());
			gui.setPlugTypeFor(i, plugs.getPlug(i).getPlugType());
		}
	}
	
	private void start(){
		updateTime();
		//todo main program loop
	}
	
	
	private void updateTime(){
		gui.setTimeClockGUI(timeService.getCurrent24Hour(), timeService.getCurrentMinutes());
	}
	
	public void editOnOffCycle(int plugNumb, int start, int end, boolean statusOn){
		//edit plug data model
		plugs.getPlug(plugNumb).editOnOffCycle(start, end, statusOn);
		
		//save plug models to datafile
		fileIO.writePlugData(plugs);
		
		//update gui with new onOffString
		gui.setOnOffStringFor(plugNumb, plugs.getPlug(plugNumb).getPlugString());
	}
	
		
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		core = new PowerBarCore();
		core.start();
	}

}
